# -*- coding:utf-8 -*-
from sklearn import svm
from sklearn import model_selection as ms
import numpy as np

# 读取数据
def iris_type(s):
    it = {'Iris-setosa':0, 'Iris-versicolor':1, 'Iris-virginica':2}
    return it[s]

path = u'./data/iris.data'
data = np.loadtxt(path, dtype=float, delimiter=',', converters={4:iris_type})
# print data

# test and train set
x, y =np.split(data, (4,), axis=1)
x = x[:, :2] #取前两列特征，方便画图
x_train, x_test, y_train, y_test = ms.train_test_split(x, y, random_state = 1, train_size = 0.6)

# train
# clf = svm.SVC(C = 0.1, kernel='linear', decision_function_shape='ovr')
clf = svm.SVC(C = 0.8, kernel='rbf', gamma=20, decision_function_shape='ovr')
clf.fit(x_train, y_train.ravel())

# 计算svc分类器的准确率
print clf.score(x_train, y_train)
y_hat = clf.predict(x_train)

# show_accuracy(y_hat, y_train, '训练集')
print clf.score(x_test, y_test)
y_hat = clf.predict(x_test)